/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import gui.panels.*;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.tree.DefaultTreeCellRenderer;

/**
 *
 * @author Memet
 */
public class MainMenu extends javax.swing.JFrame {

    LayerPanel layerPanel;
    FiberEpoxyPanel fiberEpoxyPanel;
    LaminatePanel laminatePanel;
    JPanel operations;
    StartPanel startPanel;
    AddFiber addFiber;
    AddEpoxy addEpoxy;
    AddLaminate newLaminateForm;
    
    AddLayer newLayerForm;
    boolean openForm;

    public MainMenu() {
        initComponents();
        
        layerPanel = null;
        fiberEpoxyPanel = null;
        laminatePanel = null;
        addFiber = null;
        addEpoxy = null;
        openForm = true;
        operations = new EmptyOperationPanel();
        startPanel = new StartPanel();
        spnlMain.setViewportView(startPanel);
        
        DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
        ImageIcon icon = new ImageIcon();
        renderer.setLeafIcon(icon);
        renderer.setOpenIcon(icon);
        renderer.setClosedIcon(icon);
        treeNav.setCellRenderer(renderer);
        

        openForm = true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        treeNav = new javax.swing.JTree();
        spnlMain = new javax.swing.JScrollPane();
        spnlOperations = new javax.swing.JScrollPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mbtnSave = new javax.swing.JMenuItem();
        btnExitZonderSave = new javax.swing.JMenuItem();
        mbtnExit = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        mbtnHelp = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Laminate Calculation Program");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Fiber & Epoxy");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Add Fiber");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Add Epoxy");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Layer");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Add Layer");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Export Layers");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Laminate");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Add Laminate");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Export Laminates");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNav.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        treeNav.addTreeWillExpandListener(new javax.swing.event.TreeWillExpandListener() {
            public void treeWillCollapse(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
                treeNavTreeWillCollapse(evt);
            }
            public void treeWillExpand(javax.swing.event.TreeExpansionEvent evt)throws javax.swing.tree.ExpandVetoException {
                treeNavTreeWillExpand(evt);
            }
        });
        treeNav.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeNavMouseClicked(evt);
            }
        });
        treeNav.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeNavValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(treeNav);

        spnlMain.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        spnlMain.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        spnlOperations.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        spnlOperations.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

        jMenu1.setText("File");

        mbtnSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mbtnSave.setText("Save data");
        mbtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mbtnSaveActionPerformed(evt);
            }
        });
        jMenu1.add(mbtnSave);

        btnExitZonderSave.setText("Exit without save");
        btnExitZonderSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitZonderSaveActionPerformed(evt);
            }
        });
        jMenu1.add(btnExitZonderSave);

        mbtnExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        mbtnExit.setText("Exit");
        mbtnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mbtnExitActionPerformed(evt);
            }
        });
        jMenu1.add(mbtnExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        mbtnHelp.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        mbtnHelp.setText("Open Help");
        mbtnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mbtnHelpActionPerformed(evt);
            }
        });
        jMenu2.add(mbtnHelp);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE)
                    .addComponent(spnlOperations)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(spnlMain)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spnlOperations, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void treeNavMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeNavMouseClicked
        if (treeNav.getSelectionCount() > 0 && openForm) {
            if (treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Fiber & Epoxy, Add Fiber]")) {

                if (addFiber == null) {
                    addFiber = new AddFiber(fiberEpoxyPanel);
                    addFiber.setVisible(true);
                } else {
                    if (!addFiber.isDisplayable()) {
                        addFiber = new AddFiber(fiberEpoxyPanel);
                        addFiber.setVisible(true);
                    }
                }
            } else if (treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Fiber & Epoxy, Add Epoxy]")) {
                if (addEpoxy == null) {
                    addEpoxy = new AddEpoxy(fiberEpoxyPanel);
                    addEpoxy.setVisible(true);
                } else {
                    if (!addEpoxy.isDisplayable()) {
                        addEpoxy = new AddEpoxy(fiberEpoxyPanel);
                        addEpoxy.setVisible(true);
                    }
                }
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Layer, Add Layer]"))
            {
                if(newLayerForm == null)
                {
                    newLayerForm = new AddLayer();
                    newLayerForm.setVisible(true);
                }
                else
                {
                    if(!newLayerForm.isDisplayable())
                    {
                        newLayerForm = new AddLayer();
                        newLayerForm.setVisible(true);
                    }
                }
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Layer, Export Layers]"))
            {
                ExportMatsToAnsys exportform = new ExportMatsToAnsys();
                exportform.setVisible(true);
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Laminate, Add Laminate]"))
            {
                if(newLaminateForm == null)
                {
                    newLaminateForm = new AddLaminate();
                    newLaminateForm.setVisible(true);
                }
                else
                {
                    if(!newLaminateForm.isDisplayable())
                    {
                        newLaminateForm = new AddLaminate();
                        newLaminateForm.setVisible(true);
                    }
                }
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Laminate, Export Laminates]"))
            {
                ExportLamToAnsys exportform = new ExportLamToAnsys();
                exportform.setVisible(true);
            }
        }
        openForm = true;
    }//GEN-LAST:event_treeNavMouseClicked

    private void treeNavTreeWillCollapse(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_treeNavTreeWillCollapse
        openForm = false;
    }//GEN-LAST:event_treeNavTreeWillCollapse

    private void treeNavTreeWillExpand(javax.swing.event.TreeExpansionEvent evt) throws javax.swing.tree.ExpandVetoException {//GEN-FIRST:event_treeNavTreeWillExpand
        openForm = false;
    }//GEN-LAST:event_treeNavTreeWillExpand

    private void treeNavValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeNavValueChanged
        if(treeNav.getSelectionCount() > 0)
        {
            if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Fiber & Epoxy]"))
            {
                fiberEpoxyPanel = new FiberEpoxyPanel();
                spnlMain.setViewportView(fiberEpoxyPanel);
                
                spnlOperations.setViewportView(fiberEpoxyPanel.getOperations());
                treeNav.expandPath(treeNav.getSelectionPath());
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Fiber & Epoxy, Add Epoxy]"))
            {
                addEpoxy = new AddEpoxy(fiberEpoxyPanel);
                addEpoxy.setVisible(true);
            }
             else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Fiber & Epoxy, Add Fiber]"))
            {
                addFiber = new AddFiber(fiberEpoxyPanel);
                addFiber.setVisible(true);
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Layer]"))
            {
                layerPanel = new LayerPanel();
                spnlMain.setViewportView(layerPanel);
                treeNav.expandPath(treeNav.getSelectionPath());
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Layer, Add Layer]"))
            {
                System.out.println("VAL CHANGED");
                newLayerForm = new AddLayer();
                newLayerForm.setVisible(true);
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Layer, Export Layers]"))
            {
                
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Laminate]"))
            {
                laminatePanel = new LaminatePanel();
                spnlMain.setViewportView(laminatePanel);
                treeNav.expandPath(treeNav.getSelectionPath());
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Laminate, Add Laminate]"))
            {
                newLaminateForm = new AddLaminate();
                newLaminateForm.setVisible(true);
            }
            else if(treeNav.getSelectionPath().toString().equals("[Laminate Calculation Program, Laminate, Export Laminates]"))
            {
                
            }
            else
            {
                startPanel = new StartPanel();
                spnlMain.setViewportView(startPanel);
                operations = new EmptyOperationPanel();
                spnlOperations.setViewportView(operations);
            }
        }
        openForm = true;
    }//GEN-LAST:event_treeNavValueChanged

    private void mbtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mbtnSaveActionPerformed
        //todo refactor
    }//GEN-LAST:event_mbtnSaveActionPerformed

    private void btnExitZonderSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitZonderSaveActionPerformed
        int save = JOptionPane.showConfirmDialog(rootPane, "Are you sure you don't want to save?");
        if (save == 0) {
            System.exit(0);
        } else if (save == 1) {
            /* tpdp refactor
            Serializer.Save(Global.data);
            */
            System.exit(0);
        } else {
        }
    }//GEN-LAST:event_btnExitZonderSaveActionPerformed

    private void mbtnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mbtnExitActionPerformed
        //todo refacor
        //Serializer.Save(Global.data);
        System.exit(0);
    }//GEN-LAST:event_mbtnExitActionPerformed

    private void mbtnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mbtnHelpActionPerformed

   }//GEN-LAST:event_mbtnHelpActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem btnExitZonderSave;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem mbtnExit;
    private javax.swing.JMenuItem mbtnHelp;
    private javax.swing.JMenuItem mbtnSave;
    private javax.swing.JScrollPane spnlMain;
    private javax.swing.JScrollPane spnlOperations;
    private javax.swing.JTree treeNav;
    // End of variables declaration//GEN-END:variables

    public void setOperations(JPanel panel)
    {
        operations = panel;
        spnlOperations.setViewportView(panel);
    }

    public void editEpoxyButton(boolean b)
    {
        ((EpoxyOperationPanel)operations).BtnEdit(b);
    }

    public void refreshScreens()
    {
        if(fiberEpoxyPanel != null)
        {
            fiberEpoxyPanel.updateContents();
        }
        if(laminatePanel != null)
        {
            laminatePanel.updateForm();
        }
        if(layerPanel != null)
        {
            layerPanel.updateForm();
        }
        
    }

    public FiberEpoxyPanel getFiberEpoxyPane() {
        return fiberEpoxyPanel;
    }

    public LaminatePanel getLaminatePane() {
        return laminatePanel;
    }

    public LayerPanel getLayerPane() {
        return layerPanel;
    }

    void editFiberButton(boolean b) {
        ((FiberOperationPanel)operations).BtnEdit(b);
    }

    void editMaterialButton(boolean b) {
        ((LayerOperationPanel)operations).BtnEdit(b);
    }

    void deselectNewLaminate() {
        treeNav.setSelectionPath(null);
    }
}
